package movenment2;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Menu extends JPanel{
	// static fields:
    private static final String VERSION = "Version 2.0";
    
	public Menu(ActionListener action, ActionListener action2 ){
		makeFrame(action,action2);
	}
	
	/**
     * Create the Swing frame and its content.
     */
    private void makeFrame(ActionListener action, ActionListener action2)
    {
        
     // Specify the layout manager with nice spacing
        setLayout(new BorderLayout(6, 6));
        JLabel filenameLabel = new JLabel();
        add(filenameLabel, BorderLayout.NORTH);

        JLabel statusLabel = new JLabel(VERSION);
        add(statusLabel, BorderLayout.SOUTH);
        
        final JLabel menuThings = new JLabel();
        add(menuThings, BorderLayout.CENTER);
        
        menuThings.setLayout(new BoxLayout(menuThings, BoxLayout.PAGE_AXIS));
        
        JLabel secondMenu = new JLabel();
        menuThings.add(secondMenu);
        createAButton("Play", menuThings, action);
        createAButton("Highscore", menuThings, action2);
        makeMenuBar(this);
        
        setVisible(true);   
    }
    
    /**
     * Method used to create buttons for the JPanels.
     * @param text
     * @param container
     * @param action
     */
    private static void createAButton(String text, Container container, ActionListener action) {
        JButton button = new JButton(text);
        button.setSize(100, 25);
        button.setMaximumSize(new Dimension(100, 30));
        button.setMinimumSize(new Dimension(100, 30));
        button.setPreferredSize(new Dimension(100, 30));
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        button.addActionListener(action);
        container.add(button);
    }

    /**
     * Adds the top manu bar to the frame and adds a hotkey.
     * @param frame
     */
    private void makeMenuBar(JPanel frame){
    	final int SHORTCUT_MASK =
    			Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    	JMenuBar menubar = new JMenuBar();
        frame.add(menubar,BorderLayout.NORTH);
        
        JMenu menu;
        JMenuItem item;
     // create the File menu
        menu = new JMenu("File");
        menubar.add(menu);

        item = new JMenuItem("Quit");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, SHORTCUT_MASK));
        item.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) { quit(); }
        });
        menu.add(item);

    // create the Help menu
    menu = new JMenu("Help");
    menubar.add(menu);
    
    item = new JMenuItem("About Snake...");
        item.addActionListener(new ActionListener() {
                           public void actionPerformed(ActionEvent e) { showAbout(); }
                       });
    menu.add(item);
    }
    /**
     * Shows information about the program.
     */
    private void showAbout()
    {
        JOptionPane.showMessageDialog(this, 
                    "Snake\n" + VERSION,
                    "About Snake", 
                    JOptionPane.INFORMATION_MESSAGE);
    }
    
    /**
     * Quit function: quit the application.
     */
    private void quit()
    {
        System.exit(0);
    }
}
